< previous page page_178 next page >

Page 178
   Dim offset As Long
   Dim ThisLen As Long
   If ptr = 0 Then Exit Function ' Error
   Do
      ' Find the length of this string
      ThisLen = lstrlen(offset + ptr)
      ' Set the next offset to the byte after the null character
      offset = offset + ThisLen + 1
      If ThisLen = 0 Then
         ' This will be true for two consecutive NULL characters
         FindDoubleNull2 = offset
         Exit Function
      End If
   Loop While True
End Function
There is an even faster way to perform this operation. The apigid32.dll DLL (on the CD that comes with this book) includes the function agGetStringFrom2NullBuffer, which is declared as follows:
Declare Function agGetStringFrom2NullBuffer Lib "apigid32.dll" _
(ByVal Ptr As Long) As String
This function takes as a parameter a pointer to a buffer containing NULL-terminated strings where the final string has two NULLsexactly the format used by the GetEnvironmentStrings function. It returns a Visual Basic string. But waitisn't there a problem with a DLL function returning a string? Don't worry, this function is designed specifically to return a BSTR allocated by the OLE subsystemexactly the format Visual Basic expects. The agGetStringFrom2NullBuffer function does not include the final two NULL-terminating characters in the string that it returns, so if you take this approach you'll need to modify the loop to retrieve the final string in the buffer, since it won't be detected by the lstrlen function.

 
< previous page page_178 next page >